// pages/agent/code.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    imgs:[],
  },
  onLoad(){
    this.load();
  },
  slideDone(e){
    this.setData({
      index:e.detail.current
    })
  },
  bigPic(e){
    let index=e.currentTarget.dataset.index;
    wx.previewImage({
      urls:this.data.imgs,
      current:this.data.imgs[index]
    })
  },
  async load(){
    let res=await wx.util.post("agent/getCode",{},false);
    this.setData({
      imgs:res.data.imgs
    })
  },
  savePic(){
    let that=this;
    wx.util.saveToPhoto(()=>{
      wx.showLoading({
        title: '加载中...'
      });
      wx.downloadFile({
        url: that.data.imgs[that.data.index],
        success: function (res) {
          wx.hideLoading();
          var filePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: filePath,
            success: function () {
              console.log('save success');
              wx.util.toast('保存成功')
            },
          });
        },
        fail:function(){
          wx.hideLoading();
          wx.util.toast('保存失败')
        }
      });
    })
  }
})